<style type="text/css">
#s-Div{display:none}
#s-Div div{margin-bottom:10px;}
</style>
<div class="col-xs-12" id="s-Div">
	<div class="input-group col-xs-12">
		<span class="input-group-addon">左笼</span>
		<input class="text form-control" style="display:none"/>
	  	<select class="form-control BindingTerm" id="BindingTerm_L" data-carg="0">
	  		<option value="">请选择</option>
		</select>
		<span class="input-group-btn" style="display:none"><button class="btn btn-info" data-carg="0" onclick="UnbindingTerm_SJJ(this)">解绑</button></span>
	</div>
	<div class="input-group col-xs-12">
		<span class="input-group-addon">右笼</span>
		<input class="text form-control" style="display:none"/>
	  	<select class="form-control BindingTerm" id="BindingTerm_R" data-carg="1">
	  		<option value="">请选择</option>
		</select>
		<span class="input-group-btn" style="display:none"><button class="btn btn-info" data-carg="1" onclick="UnbindingTerm_SJJ(this)">解绑</button></span>
	</div>
</div>
<div class="col-xs-12" style="margin:5px" id="t-Div">
	<select class="form-control BindingTerm" id="BindingTerm_T">
		<option value="">请选择</option>
	</select>
</div>
<script type="text/javascript">
	initBindingTerm();
	function initBindingTerm(){
		if(em.Operation=="UnBinding"){
				Dialog.saveBtn.bind("click",function(){
				Dialog.exit();
				if($("#main").find("#eqptDataGrid").length>0){
					$("#main").empty().load("../eqpt/eqpt.html");
				}else{
					$("#main").empty().load("../eqpt/eqpt_overView.html");
				}		
			});
			}else{
				for(var i=0;i<em.TermList.length;i++){
					$(".BindingTerm").each(function(){
						$(this).append("<option value='"+em.TermList[i].termCode+"'>" + em.TermList[i].termCode+ "</option>");
					});
				}
				Dialog.saveBtn.bind("click",function(){
					EqptBindingSave();
				});
			}
		if(em.EqptType=="升降机"){
			$("#s-Div").show();
			$("#t-Div").hide();
			callSvr("eqpt/getBindStatusBySJJ",{"eqptId":em.EqptId},function(result){
				if(typeof(result.err)=="undefined"){
					for(var i=0;i<result.length;i++){
						var Select=$(".BindingTerm").filter("[data-carg='"+result[i].Carg+"']");
						Select.prev().val("已绑定:"+result[i].TermCode).show().attr("disabled",true);	
						Select.next().show().find("button").data("term",result[i].TermCode);
						Select.hide();
					}
				}
			});
		}
	}
	function EqptBindingSave(){
		if(em.EqptType=="升降机"){
			var tL=$("#BindingTerm_L").val();
			var tR=$("#BindingTerm_R").val();
			if((tL=="" && tR=="") || ($("#BindingTerm_L").is(':hidden') && tR=="") || ($("#BindingTerm_R").is(':hidden') && tL=="")){
				alert("请选择绑定终端");
				return;
			}
			if(tL==tR){
				alert("左/右笼不能绑定同一台终端!");
				return;
			}
			if(tL!=""){
				Binding(tL,"0");
			}
			if(tR!=""){
				Binding(tR,"1");
			}
		}else{
			var termCode=$("#BindingTerm_T").val();
			Binding(termCode,"0");
		}
		Dialog.exit();
		if($("#main").find("#eqptDataGrid").length>0){
			showEqptList();
		}else{
			$("#main").empty().load("../eqpt/eqpt_overView.html");
			unbind();
		}
	}
	function Binding(termCode,carg){
		callSvr("eqpt/TermBinding",{"eqptId":em.EqptId,"termCode":termCode,"eqptCode":em.EqptCode,"carg":carg,"etype":em.EqptType},function(result){
			if (result.err == 0) {
				toastr.success('绑定成功', '');
				showEqptList();
			} else if(result.err == 1){
				toastr.error('终端返回失败', '');
			}else if(result.err == 4){
				toastr.error('终端不在线', '');
			}else if(result.err == 6){
				toastr.error('该终端繁忙,稍后重试', '');
			}else{
				toastr.error('服务器连接失败,请重新尝试', '');
			}
		});
	};
	function UnbindingTerm_SJJ(e){
		em.TermCode=$(e).data("term");
		var carg=$(e).data("carg");
		callSvr("eqpt/TermUnbundling",{"eqptId":em.EqptId,"termCode":em.TermCode,"eqptCode":em.EqptCode,"carg":carg},function(result){
			if (result.err == 0) {
				toastr.success('解绑成功', '');
				$(e).parent().hide().prev().show().prev().hide();
				showEqptList();
			} else if(result.err == 1){
				toastr.error('终端返回失败', '');
			}else if(result.err == 4){
				toastr.error('终端不在线', '');
			}else if(result.err == 6){
				toastr.error('该终端繁忙,稍后重试', '');
			}else{
				toastr.error('服务器连接失败,请重新尝试', '');
			}
		});
	};
</script>